var Shares = require('gameshare');
var GameObject = Shares.GameObject;
var GameEvent = Shares.GameEvent;
var GameConst = Shares.GameConst;

cc.Class({
    extends: cc.Component,

    properties: {
        objectType: {
            type:GameObject,
            default: GameObject.StaticTrap
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    onLoad: function () {
        this.halfsize = GameConst.halfsize;
        this.size = GameConst.size;
        this.width = GameConst.width;
        this.height = GameConst.height;
        cc.systemEvent.on(GameEvent.AddBlock, this.refresh, this);
        this.available = true;
        this.trapSprite = this.node.getChildByName('trap');
        this.unAvailableSprite = this.node.getChildByName('unavailable');
    },

    onDestroy () {
        cc.systemEvent.off(GameEvent.AddBlock, this.refresh, this);
    },

    start () {    
        this.alineToLines();
        this.screenPos = this.node.convertToWorldSpaceAR(cc.v2(0, 0));
        this.refresh();
    },

    alineToLines: function(){
        var x = this.node.x - this.halfsize;
        x = Math.floor(x/this.size)*(this.size) + this.halfsize;
        var y = this.node.y - this.halfsize;
        y = Math.floor(y/this.size)*(this.size) + this.halfsize;
        this.node.x = x;
        this.node.y = y;
    },

    refresh(){
        var map = cc.zsb.map.map;
        var xidx = Math.floor(this.node.x / this.size);
        var yidx = Math.floor(this.node.y / this.size);
        this.eageFlag = [false,false,false,false];
        //up left down right
        var eagecount = 0;
        var trueEnge = '';

        var idx = xidx + (yidx + 1)*this.width;
        if( yidx < (this.height  - 1) && map[idx] && map[idx].data.objectType == GameObject.StaitcBlock){
            this.eageFlag[0] = true;
            eagecount++;
            trueEnge = 'up';
        }

        idx = (xidx - 1)  + yidx * this.width;
        if( xidx > 0 && map[idx] && map[idx].data.objectType == GameObject.StaitcBlock){
            this.eageFlag[1] = true;
            eagecount++;
            trueEnge = 'left';
        }

        idx = xidx + (yidx - 1)*this.width;
        if( yidx > 0 && map[idx] && map[idx].data.objectType == GameObject.StaitcBlock){
            this.eageFlag[2] = true;
            eagecount++;
            trueEnge = 'down';
        }

        idx = (xidx + 1)  + yidx * this.width;
        if( xidx < (this.width - 1) && map[idx] && map[idx].data.objectType == GameObject.StaitcBlock){
            this.eageFlag[3] = true;
            eagecount++;
            trueEnge = 'right';
        }

        if(eagecount == 1){
            this.available = true;
            if(trueEnge == 'up'){
                this.trapSprite.rotation = 180;
                this.trapSprite.position = cc.v2(0, 20);
            }
            else if(trueEnge == 'down'){
                this.trapSprite.rotation = 0;
                this.trapSprite.position = cc.v2(0, -20);
            }
            else if(trueEnge == 'left'){
                this.trapSprite.rotation = -90;
                this.trapSprite.position = cc.v2(-20, 0);
            }
            else if(trueEnge == 'right'){
                this.trapSprite.rotation = 90;
                this.trapSprite.position = cc.v2(20, 0);
            }
        }
        else{
            this.available = false;
        }

        //this.unAvailableSprite.active = this.available;

    },

    setData: function(data){
        this.node.position = data.position;
        this.refresh();
    },
    
    toData: function(){
        var data = {
            objectType: this.objectType,
            eageFlag: this.eageFlag,
            position: this.node.position,
        }

        return data;
    },

    isInRect: function (pos) {
        var posscreen = this.screenPos;
        if (pos.x < posscreen.x + this.halfsize &&
            pos.x > posscreen.x - this.halfsize &&
            pos.y < posscreen.y + this.halfsize &&
            pos.y > posscreen.y - this.halfsize) {
            return true;
        }

        return false
    },

    update: function (dt) {
        if (cc.zsb && cc.zsb.player) {
            var player = cc.zsb.player;
            if (player && player.isPause == false) {
                var collide = false;
                var playerScreenPos = player.getScreenPos();
                if (this.isInRect(playerScreenPos)) {
                    collide = true
                }

                if (collide) {
                    var playerDir = cc.v2(-player.dir.x, -player.dir.y);
                    player.doAfterCollide(this.node, playerDir, this.objectType);
                }
            }
        }
    },
});
